<template>
    <div>

        <template>

            <Tabs type="card" @on-click="tab">
                <TabPane label="新闻分类">
                    <Tree :data="data" :render="renderContent" ></Tree>
                </TabPane>
                <TabPane label="上传分类">
                    <Tree :data="data" :render="renderContent" ></Tree>
                </TabPane>
                <TabPane label="产品分类">
                    <Tree :data="data" :render="renderContent" ></Tree>
                </TabPane>
            </Tabs>
        </template>

        <!-- <Tree :data="data" :render="renderContent"></Tree> -->

        <Modal v-model="modal" :title="changeTitle">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                <FormItem label="父元素ID" prop="parentId" >
                    <Input v-model="formValidate.parentId" readonly></Input>
                </FormItem>
                <FormItem label="分类名字" prop="text">
                    <Input v-model="formValidate.title" placeholder="请输入姓名"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                    <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                </FormItem>
            </Form>  
        </Modal>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                data: [
                    {
                        title: "总分类",
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%',
                                    fontSize:"18px",
                                    cursor: 'pointer',
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'ios-folder-outline'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '32px'
                                    }
                                }, [
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-plus-empty',
                                            type: 'primary'
                                        }),
                                        style: {
                                            width: '85px'
                                        },
                                        on: {
                                            click: () => { this.append(data) }
                                        }
                                    })
                                ])
                            ]);
                        },
                        children: [],
                    }
                ],
                buttonProps: {
                    type: 'ghost',
                    size: 'small',
                },
                modal:false,
                formValidate: {
                    parentId: '',
                    title:'',
                    type: 0,
                },
                ruleValidate: {
                    title: [
                        { required: true, message: '名称不能为空', trigger: 'blur' },
                    ],
                },
                changeTitle:"",
                type: 0,
            }
        },
        methods: {
            getData(){
                this.$http.get(`http://127.0.0.1:3000/cate/list/${this.type}`)
                .then(res=>{
                    console.log(res.data);
                    this.data[0].children = res.data
                })
            },
            renderContent (h, { root, node, data }) {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%',
                        fontSize:"18px",
                        cursor: 'pointer',
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'ios-paper-outline'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-plus-empty'
                            }),
                            style: {
                                marginRight: '8px'
                            },
                            on: {
                                click: () => { this.append(data) }
                            }
                        }),
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'edit'
                            }),
                            style: {
                                marginRight: '8px'
                            },
                            on: {
                                click: () => { this.updata(data) }
                            }
                        }),
                        h('Button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'ios-minus-empty'
                            }),
                            on: {
                                click: () => { this.remove(root, node, data) }
                            }
                        })
                    ])
                ]);
            },
            append (data) {
                this.modal = true,
                this.changeTitle = "添加分类"
                console.log(data._id);
                this.formValidate = {};
                this.formValidate.parentId = data._id;
                this.formValidate.type = this.type;
            },
            updata (data){
                this.modal = true;
                this.changeTitle = "修改名称"
                Object.assign(this.formValidate,data);
            },
            remove (root, node, data) {
                console.log(data._id);
                 this.$Modal.confirm({
                    title: '删除提示',
                    content: '<p>确定要删除该记录？</p>',
                    onOk: () => {
                        this.$http.delete("http://127.0.0.1:3000/cate/data/"+data._id)
                        .then(res=>{
                            console.log(res);
                            this.getData();
                        })
                    },
                    onCancel: () => {
                        // this.$Message.info('点击了取消');
                    }
                })
            },
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    console.log(this.formValidate);
                    if (valid) {
                        if(this.formValidate._id){
                            this.$http.put("http://127.0.0.1:3000/cate/data/"+this.formValidate._id,this.formValidate)
                            .then(res=>{
                                console.log(res);
                                this.getData();
                                this.modal = false;
                            })
                        }else{
                            this.$http.post("http://127.0.0.1:3000/cate/data",this.formValidate)
                            .then(res=>{
                                this.getData();
                                this.modal = false;
                            })
                        }
                    } else {
                        this.$Message.error('表单验证失败!');
                    } 
                });
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            tab(res){
                this.type = res;
                console.log(res);
                this.getData();
            }
        },
        created(){
           this.getData();
        }
    }
</script>
<style scoped>
    
</style>
